﻿using System;
using System.Collections.Generic;
using NHibernate.Mapping.ByCode;
using Xenta.Enums;

namespace Xenta.Entities
{
    /// <summary>
    /// Represents the author entity.
    /// </summary>
    /// <remarks><pre>
    /// The entity regarding the disclosure level.
    /// 
    /// + Entire
    ///  + Extended
    ///   + Base
    ///    | - PublicationCount
    ///    | - RatingAverage
    ///    | - Flags
    ///    | - UpdatedOn
    ///    | - CreatedOn
    ///    + Keys
    ///     | - Account(of the same level)
    ///     + Primary
    ///      | - EntityID
    /// 
    /// Note: upper level includes lower level.
    /// </pre></remarks>
    public class PublicationAuthorEntity : AccountProfileEntity
    {
        #region Fields

        private ICollection<PublicationEntity> _publications;

        #endregion

        #region Ctors

        /// <summary>
        /// Initializes a new class instance.
        /// </summary>
        public PublicationAuthorEntity()
        {
            _publications = new List<PublicationEntity>();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the number of active publications.
        /// </summary>
        public virtual int PublicationCount
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the average rating.
        /// </summary>
        public virtual byte RatingAverage
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the flags.
        /// </summary>
        public virtual PublicationAuthorFlags Flags
        {
            get;
            set;
        }

        #endregion

        #region Collections

        /// <summary>
        /// Gets or sets the publication collection.
        /// </summary>
        public virtual ICollection<PublicationEntity> Publications
        {
            get
            {
                return _publications;
            }
            set
            {
                _publications = value;
            }
        }

        #endregion

        #region Utilities

        protected override void Disclose(IDictionary<String, Object> obj,
            DisclosureLevel level,
            IDictionary<String, String> options)
        {
            base.Disclose(obj, level, options);
            if(level >= DisclosureLevel.Base)
            {
                obj["PublicationCount"] = PublicationCount;
                obj["RatingAverage"] = RatingAverage;
                obj["Flags"] = Flags.ToString();
            }
        }

        #endregion
    }

    /// <summary>
    /// The entity NHibernate mapping.
    /// </summary>
    public class PublicationAuthorMapping : EntityMapping<PublicationAuthorEntity>
    {
        /// <summary>
        /// Initializes a new class instance.
        /// </summary>
        public PublicationAuthorMapping()
        {
            Table("fwk_PublicationAuthors");
            Id(x => x.EntityID, m =>
            {
                m.Column("AuthorID");
                m.Generator(Generators.Identity);
            });
            ManyToOne(x => x.Account, m =>
            {
                m.Column("AccountID");
                m.ForeignKey("FK_fwk_PublicationAuthors_fwk_Accounts");
                m.Index("IX_fwk_PublicationAuthors");
                m.NotNullable(true);
                m.Fetch(FetchKind.Join);
            });
            Property(x => x.PublicationCount, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.RatingAverage, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.Flags, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.CreatedOn, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.UpdatedOn, m =>
            {
                m.NotNullable(true);
            });
            Bag(x => x.Publications, m =>
            {
                m.Table("fwk_Publications");
                m.Key(k =>
                {
                    k.Column("AuthorID");
                    k.ForeignKey("FK_fwk_Publications_fwk_PublicationAuthors");
                    k.NotNullable(true);
                });
                m.Inverse(true);
            },
            a =>
            {
                a.OneToMany();
            });
            Cache(m =>
            {
                m.Usage(CacheUsage.ReadWrite);
            });
        }
    }
}
